import { Controller, Get, Post, Body, Param, Delete, DefaultValuePipe, Query } from '@nestjs/common';
import { MeetingRoomService } from './meeting-room.service';
import { generateParseIntPipe } from '../utils';
import { CreateMeetingRoomDto } from './dto/create-meeting-room.dto';
import { UpdateMeetingRoomDto } from './dto/update-meeting-room.dto';
import { ApiBearerAuth, ApiExcludeEndpoint, ApiQuery, ApiQueryOptions, ApiTags } from '@nestjs/swagger';
import { RequireLogin } from '../custom.decorator';


@Controller('meeting-room')
@ApiBearerAuth()
@ApiTags('会议室预约')
export class MeetingRoomController {
  constructor(private readonly meetingRoomService: MeetingRoomService) { }

  @Get('list')
  @RequireLogin()
  @ApiQuery({ name: 'pageNo', type: Number, description: '页码', example: 1, minimum: 1 } as ApiQueryOptions)
  @ApiQuery({ name: 'pageSize', type: Number, description: '每页数量', example: 10 })
  @ApiQuery({ name: 'name', type: String, required: false, })
  @ApiQuery({ name: 'capacity', type: String, required: false })
  @ApiQuery({ name: 'equipment', type: String, required: false })
  async list(
    @Query('pageNo', new DefaultValuePipe(1), generateParseIntPipe('pageNo')) pageNo: number,
    @Query('pageSize', new DefaultValuePipe(2), generateParseIntPipe('pageSize')) pageSize: number,
    @Query('name') name: string,
    @Query('capacity') capacity: number,
    @Query('equipment') equipment: string
  ) {
    return await this.meetingRoomService.find(pageNo, pageSize, name, capacity, equipment);
  }

  @Get(':id(\\d+)')
  @RequireLogin()
  async find(@Param('id') id: number) {
    return await this.meetingRoomService.findById(+id);
  }

  @Delete(':id(\\d+)')
  @RequireLogin()
  async delete(@Param('id') id: number) {
    return await this.meetingRoomService.delete(+id);
  }

  @Post('update')
  @RequireLogin()
  async update(@Body() meetingRoomDto: UpdateMeetingRoomDto) {
    return await this.meetingRoomService.update(meetingRoomDto);
  }

  @Post('create')
  @RequireLogin()
  async create(@Body() meetingRoomDto: CreateMeetingRoomDto) {
    return await this.meetingRoomService.create(meetingRoomDto);
  }

  // 一定要注意上面的:id要做类型限制,否则会匹配失败
  @ApiExcludeEndpoint() //不显示在swagger
  @Get("init-data") async initData() { 
    await this.meetingRoomService.initData(); 
    return 'done';
   }

}
